SELECT A.ORDER_FLAG,
       A.ORDER_TITLE,
       A.ORDER_SN,
       A.ORDER_TYPE,
       A.ORDER_NAME,
       A.ORDER_TYPE_NAME,
       A.ORDER_TIME,
       A.CANCEL_TIME,
       A.ORDER_DEPT,
       A.ORDER_DEPT_name,
       A.Confirm_Person_Name,
       A.EXEC_DEPT,
       A.EXEC_DEPT_name,
       A.PATIENT_SN,
       A.WARDS_ID,
       A.ward_name,
       A.TEMPORARY_FLAG,
       A.Confirm_Time,
       A.CANCEL_PERSON_NAME,
       A.CANCEL_PERSON,
       A.order_person_name
  FROM (
        --医嘱标识，医嘱类型名称，医嘱SN, 医嘱类型，医嘱名称，医嘱类型名称，开嘱时间，取消时间，下医嘱科室，
		--下医嘱科室名称，下医嘱人ID, 下医嘱人，医嘱确认人ID，医嘱确认人，执行科室，执行科室名称，病人编码，病区，病区名称，长期或临时，确认时间，取消人，取消人ID， 就诊SN
        SELECT '1' ORDER_FLAG,
                '诊疗医嘱' ORDER_TITLE,
                TOD.ORDER_SN,
                TOD.ORDER_TYPE,
                TOD.ITEM_NAME ORDER_NAME,
                TOD.ORDER_TYPE_NAME ORDER_TYPE_NAME,
                TOD.ORDER_TIME,
                TOD.CANCEL_TIME,
                TOD.ORDER_DEPT,
                TOD.ORDER_DEPT_name,
                TOD.ORDER_PERSON,
                TOD.ORDER_PERSON_NAME,
                TOD.Nurse_Confirm_Person,
                TOD.Nurse_Confirm_Person_Name as Confirm_Person_Name,
                TOD.EXECUTIVE_DEPT EXEC_DEPT,
                TOD.EXEC_DEPT_name,
                TOD.PATIENT_SN,
                TOD.WARDS_ID,
                TOD.ward_name,
                TOD.TEMPORARY_FLAG,
                TOD.Nurse_Confirm_Time as Confirm_Time,
                TOD.CANCEL_PERSON_NAME,
                TOD.CANCEL_PERSON,
                TOD.VISIT_SN,
                TOD.SPECIFICATION
          FROM TREATMENT_ORDER TOD --诊疗医嘱
         WHERE TOD.delete_flag = 0 and TOD.visit_sn = /*visitSn*/
        UNION all
        --医嘱标识，医嘱类型名称，医嘱SN, 医嘱类型，医嘱名称，医嘱类型名称，开嘱时间，取消时间，下医嘱科室，
		--下医嘱科室名称，下医嘱人ID, 下医嘱人，医嘱确认人ID，医嘱确认人，执行科室，执行科室名称，病人编码，病区，病区名称，长期或临时，确认时间，取消人，取消人ID， 就诊SN
        SELECT '2' ORDER_FLAG,
               '护理医嘱' ORDER_TITLE,
               CO.ORDER_SN,
               CO.ORDER_TYPE,
               CO.ORDER_NAME, --医嘱类型名称
               CO.ORDER_TYPE_NAME,
               CO.INPUT_TIME ORDER_TIME,
               CO.CANCEL_TIME,
               CO.ORDER_DEPT,
               CO.ORDER_DEPT_name,
               CO.ORDER_PERSON,
               Co.ORDER_PERSON_NAME,
               CO.Nurse_Confirm_Person,
               CO.Nurse_Confirm_Person_Name as Confirm_Person_Name,
               CO.EXEC_DEPT,
               CO.EXEC_DEPT_name,
               CO.PATIENT_SN,
               CO.WARDS_ID,
               CO.wards_name as ward_name,
               CO.USAGE TEMPORARY_FLAG,
               CO.Nurse_Confirm_Time as Confirm_Time,
               CO.CANCEL_PERSON_NAME,
               CO.CANCEL_PERSON,
               CO.VISIT_SN,
               null SPECIFICATION
          FROM CARE_ORDER CO --护理医嘱
         WHERE CO.delete_flag = 0 and CO.visit_sn = /*visitSn*/
        UNION all        
        SELECT '6' ORDER_FLAG,
               '其他医嘱' ORDER_TITLE,
               go.ORDER_SN,
               go.order_type_code as ORDER_TYPE,
               go.ORDER_NAME as ORDER_NAME,
               go.ORDER_TYPE_NAME ORDER_TYPE_NAME,
               go.INPUT_TIME ORDER_TIME,
               go.CANCEL_TIME,
               go.order_dept_id as ORDER_DEPT,
               go.ORDER_DEPT_name,
               go.order_person_id as ORDER_PERSON,
               go.ORDER_PERSON_NAME,
               go.nurse_confirm_person_id as nurse_Confirm_Person,
               go.nurse_Confirm_Person_Name as Confirm_Person_Name,
               go.exec_dept_id as exec_dept,
               go.EXEC_DEPT_name,
               go.PATIENT_SN,
               go.ward_id as WARDS_ID,
               go.ward_name,
               go.usage TEMPORARY_FLAG,
               go.nurse_Confirm_Time as Confirm_Time,
               go.CANCEL_PERSON_NAME,
               go.cancel_person_id as CANCEL_PERSON,
               go.VISIT_SN,
               null SPECIFICATION
          FROM general_order go --其他医嘱
         WHERE go.delete_flag = 0 and go.visit_sn = /*visitSn*/
        UNION all
        SELECT '7' ORDER_FLAG,
               '会诊医嘱' ORDER_TITLE,
               co.ORDER_SN,
               co.order_type_code as order_type,
               co.ORDER_NAME,
               co.ORDER_TYPE_NAME ORDER_TYPE_NAME,
               co.ORDER_TIME,
               co.CANCEL_TIME,
               co.order_dept_id as ORDER_DEPT,
               co.ORDER_DEPT_name,
               co.order_person_id as order_person,
               co.ORDER_PERSON_NAME,
               co.confirm_person_id as Confirm_Person,
               co.Confirm_Person_Name,
               '' EXEC_DEPT,
               '' EXEC_DEPT_name,
               co.PATIENT_SN,
               '' WARDS_ID,
               '' ward_name,
               --co.TEMPORARY_FLAG,
               '0' TEMPORARY_FLAG,  --临时医嘱
               co.Confirm_Time,
               co.CANCEL_PERSON_NAME,
               co.cancel_person_id as CANCEL_PERSON,
               co.VISIT_SN,
               null SPECIFICATION
          FROM consultation_order co --会诊医嘱
         WHERE co.delete_flag = 0 and co.visit_sn = /*visitSn*/
        UNION all
        SELECT '3' ORDER_FLAG,
               '手术医嘱' ORDER_TITLE,
               po.order_sn,
               po.order_type,
               po.ORDER_NAME ,
               po.ORDER_TYPE_NAME ORDER_TYPE_NAME,
               po.ORDER_TIME,
               po.CANCEL_TIME,
               po.ORDER_DEPT,
               po.ORDER_DEPT_name,
               po.order_person,
               po.ORDER_PERSON_NAME,
               po.Confirm_Person,
               po.Confirm_Person_Name,
               po.EXEC_DEPT, 
               po.EXEC_DEPT_name,
               po.PATIENT_SN,
               po.WARDS_ID,
               po.wards_name,
               po.temporary_flag,  
               po.Confirm_Time,
               po.CANCEL_PERSON_NAME,
               po.CANCEL_PERSON,
               po.VISIT_SN,
               null SPECIFICATION
          FROM procedure_order po --手术医嘱
         WHERE po.delete_flag = 0 and po.visit_sn = /*visitSn*/
		 ) A
 ORDER BY A.ORDER_TIME DESC, A.ORDER_SN DESC
